package gateway

import (
	"context"
	"github.com/go-playground/validator/v10"
	"github.com/liserc/opensocket-server/pkg/config"
	"github.com/liserc/opensocket-server/tool/discovery"
	"github.com/liserc/opensocket-server/tool/jsonutil"
	"sync"
)

type ConnectReq struct {
	Token       string `json:"token"`
	PlatformID  int    `json:"platformID"    validate:"required"`
	OperationID string `json:"operationID"   validate:"required"`
}

type RpcReq struct {
	ReqIdentifier int32  `json:"reqIdentifier" validate:"required"`
	Token         string `json:"token"`
	SendID        string `json:"sendID"        validate:"required"`
	OperationID   string `json:"operationID"   validate:"required"`
	MsgIncr       string `json:"msgIncr"       validate:"required"`
	Data          []byte `json:"data"`
}

func (r *RpcReq) String() string {
	var tmp RpcReq
	tmp.ReqIdentifier = r.ReqIdentifier
	tmp.Token = r.Token
	tmp.SendID = r.SendID
	tmp.OperationID = r.OperationID
	tmp.MsgIncr = r.MsgIncr
	return jsonutil.StructToJsonString(tmp)
}

func GetReq() *RpcReq {
	req := reqPool.Get().(*RpcReq)
	req.Data = nil
	req.MsgIncr = ""
	req.OperationID = ""
	req.ReqIdentifier = 0
	req.SendID = ""
	req.Token = ""
	return req
}

func FreeReq(req *RpcReq) {
	reqPool.Put(req)
}

type RpcResp struct {
	ReqIdentifier int32  `json:"reqIdentifier"`
	MsgIncr       string `json:"msgIncr"`
	OperationID   string `json:"operationID"`
	ErrCode       int    `json:"errCode"`
	ErrMsg        string `json:"errMsg"`
	Data          []byte `json:"data"`
}

func (r *RpcResp) String() string {
	var tResp RpcResp
	tResp.ReqIdentifier = r.ReqIdentifier
	tResp.MsgIncr = r.MsgIncr
	tResp.OperationID = r.OperationID
	tResp.ErrCode = r.ErrCode
	tResp.ErrMsg = r.ErrMsg
	return jsonutil.StructToJsonString(tResp)
}

var reqPool = sync.Pool{
	New: func() any {
		return new(RpcReq)
	},
}

type MessageFunc func(context context.Context, data *RpcReq) ([]byte, error)

type MessageHandle interface {
	GetSeq(context context.Context, data *RpcReq) ([]byte, error)
	SendMessage(context context.Context, data *RpcReq) ([]byte, error)
	SendSignalMessage(context context.Context, data *RpcReq) ([]byte, error)
	PullMessageBySeqList(context context.Context, data *RpcReq) ([]byte, error)
	GetSeqMessage(context context.Context, data *RpcReq) ([]byte, error)
	GetConversationsHasReadAndMaxSeq(context context.Context, data *RpcReq) ([]byte, error)
	UserLogout(context context.Context, data *RpcReq) ([]byte, error)
}

type GrpcMessageHandle struct {
	validate *validator.Validate
}

func NewGrpcMessageHandle(registry discovery.SvcDiscoveryRegistry, rpcRegisterName *config.RpcRegisterName) *GrpcMessageHandle {
	return &GrpcMessageHandle{
		validate: validator.New(),
	}
}

func (hd *GrpcMessageHandle) GetSeq(context context.Context, data *RpcReq) ([]byte, error) {
	return nil, nil
}

func (hd *GrpcMessageHandle) SendMessage(context context.Context, data *RpcReq) ([]byte, error) {
	return nil, nil
}

func (hd *GrpcMessageHandle) SendSignalMessage(context context.Context, data *RpcReq) ([]byte, error) {
	return nil, nil
}

func (hd *GrpcMessageHandle) PullMessageBySeqList(context context.Context, data *RpcReq) ([]byte, error) {
	return nil, nil
}

func (hd *GrpcMessageHandle) GetSeqMessage(context context.Context, data *RpcReq) ([]byte, error) {
	return nil, nil
}

func (hd *GrpcMessageHandle) GetConversationsHasReadAndMaxSeq(context context.Context, data *RpcReq) ([]byte, error) {
	return nil, nil
}

func (hd *GrpcMessageHandle) UserLogout(context context.Context, data *RpcReq) ([]byte, error) {
	return nil, nil
}
